<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="活动id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="活动id"></el-input>
    </el-form-item> -->
    <el-form-item label="活动类型" prop="activityType">
      <!-- <el-input v-model="dataForm.activityType" placeholder="活动类型: 1:直减 2:满减 3:直折 4:满折 5:每满减"></el-input> -->
      <el-select v-model="dataForm.activityType" placeholder="请选择">
        <el-option
          v-for="item in activityTypeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="满足金额" prop="amountMoney">
      <el-input style="width:23%" v-model="dataForm.amountMoney" placeholder="满足金额或折扣"></el-input>
    </el-form-item>
    <el-form-item label="优惠金额" prop="discount">
      <el-input style="width:23%" v-model="dataForm.discount" placeholder="优惠金额或折扣"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-switch
        v-model="dataForm.state"
        active-color="#13ce66"
        inactive-color="#dcdfe6">
      </el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        activityTypeOptions: [
          {
            value: 1,
            label: '直减'
          },
          {
            value: 2,
            label: '满减'
          },
          {
            value: 3,
            label: '直折'
          },
          {
            value: 4,
            label: '满折'
          },
          {
            value: 5,
            label: '每满减'
          }
        ],
        dataForm: {
          id: 0,
          parentId: '',
          activityType: '',
          amountMoney: '',
          discount: '',
          state: true,
          createdAt: '',
          updatedAt: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          activityType: [
            { required: true, message: '活动类型: 1:直减 2:满减 3:直折 4:满折 5:每满减不能为空', trigger: 'blur' }
          ],
          amountMoney: [
            { required: true, message: '满足金额或折扣不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '优惠金额或折扣不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态: 1:启用 0:禁用不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/biz/couponActivityDetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.couponActivityDetail.parentId
                this.dataForm.activityType = data.couponActivityDetail.activityType
                this.dataForm.amountMoney = data.couponActivityDetail.amountMoney
                this.dataForm.discount = data.couponActivityDetail.discount
                this.dataForm.state = data.couponActivityDetail.state
                this.dataForm.createdAt = data.couponActivityDetail.createdAt
                this.dataForm.updatedAt = data.couponActivityDetail.updatedAt
              }
            })
          }
        })
      },
      getRouteAction(action) {
        if (window.location.href.split(action + "=")[1]) {
          return window.location.href.split(action + "=")[1].split("&")[0];
        }
        return false;
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          var flag = 0;
          if (this.dataForm.state) {
              flag = 1;
          }
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz/couponActivityDetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.getRouteAction("id"),
                'activityType': this.dataForm.activityType,
                'amountMoney': this.dataForm.amountMoney,
                'discount': this.dataForm.discount,
                'state': flag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
